layui.use(['table', 'form', 'layer' ], function() {
  var table = layui.table;
  var layer = layui.layer;
  table.render({
    elem: '#config-table'
    ,title: '业务系统'
    ,url: ctx + '/dept/list'
    ,parseData: function(res){ //res 即为原始返回的数据
      return {
        "code": res.code, //解析接口状态
        "msg": res.msg, //解析提示文本
        "count": res.page.totalCount, //解析数据长度
        "data": res.page.list //解析数据列表
      };
    }
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field:'id', title:'ID',  fixed: 'left',  sort: true, width: 80}
      ,{field:'department', title:'处室名称' , width: 220}
      ,{field:'deptId', title:'部门编号'}
      ,{field:'sortIndex', title:'排序号'}
      ,{fixed: 'right', title: '操作', toolbar: '#toolbar', width: 200}
    ]]
    ,page: true
  });

  table.on('tool(config-table)', function (obj) {
    switch (obj.event) {
      case 'edit':
        layer.open({
          title: false,
          type: 2,
          area: ['650px', '610px'],
          content: ctx + 'sys/dept-edit.html?id=' + obj.data.id,
          end: function () {
            location.reload();
          }
        })
        break;
        break;
      case 'del':
        layer.open({
          content: '是否确认删除'
          ,btn: ['删除', '取消']
          ,yes: function(){
            axios.post(ctx + 'dept/delete', [obj.data.id]).then(function (res) {
              if (res.data.code == 0) {
                layer.msg('删除成功');
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                location.reload();
              } else {
                parent.layer.msg(res.data.msg);
              }

            })
          }
          ,btn2: function(index, layero){

          }
          ,cancel: function(){
            //右上角关闭回调
          }
        });
        break;
    }
  });


  $('#add-btn').click(function () {
    layer.open({
      title: false,
      type: 2,
      area: ['680px', '360px'],
      content: ctx + 'sys/dept-add.html',
      end: function () {
        location.reload();
      }
    })
  })


});





